package com.hsnn.medstgmini.base.dict.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.DicEntprodProfile;
import com.hsnn.medstgmini.base.dict.service.DicEntprodProfileManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.ExcelUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;


/**
 * @category 企业产品资质 类型
 * @author wangbing
 * @date 2015年7月13日
 */
@Controller
@RequestMapping("/dicEntprodProfile")
public class DicEntprodProfileController {

	private static final Logger log = Logger.getLogger(DicEntprodProfileController.class);
			

	@Autowired
	private DicEntprodProfileManager dicEntprodProfileManager;

	@Autowired
	private HttpServletRequest request;
	
	
	/**
	 * @category 跳转企业产品资质 类型页面
	 * @date 2015年7月13日
	 * @return String
	 */
	@RequestMapping("/toEntprodProfileList")
	public String toEntprodProfileList() {
		log.info("enter toEntprodProfileList method");
		return "/base/dict/entprodProfileList";
	}
	
	/**
	 * @category 获取企业产品资质 类型列表
	 * @date 2015年7月13日
	 * @return page
	 */
	@RequestMapping("/getDicEntprodProfileList")
	@ResponseBody
	public Pagination getDicEntprodProfileList(){
		log.info("enter getDicEntprodProfileList method");
		Pagination page = new Pagination(request);
		try {
			page.getConditions().put("indiName", request.getParameter("indiName"));
			page.getConditions().put("status", request.getParameter("status"));
			dicEntprodProfileManager.getDicEntprodProfileList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 停用启用
	 * @date 2015年7月13日
	 * @return page
	 */
	@RequestMapping("/changeStatus")
	@ResponseBody
	public Pagination changeStatus(){
		log.info("enter changeStatus method");
		Pagination page = new Pagination();
		try {
			page.getConditions().put("dicEntprodProfileId", request.getParameter("dicEntprodProfileId"));
			page.getConditions().put("status", request.getParameter("status"));
			page.getConditions().put("updUser", ((SysUser)request.getSession().getAttribute(Constants.USERINFO)).getId());
			dicEntprodProfileManager.changeStatus(null);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category 批量启用停用
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@RequestMapping(value={"/enable","/disable"})
	@ResponseBody
	public Pagination batchChangeStatus(String array) {
		log.info("enter batchChangeStatus method");
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		try {
			if (array != null && !"".equals(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<DicEntprodProfile>>() {
				}.getType();
				List<DicEntprodProfile> list = gson.fromJson(array, type);
				if (list != null && list.size()>0) {
					for (DicEntprodProfile dds : list) {
						dds.setUpdUser(user.getId());
						dicEntprodProfileManager.changeStatus(dds);
					}
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 初始化编辑数据
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@RequestMapping("/initEdit")
	@ResponseBody
	public Pagination initEdit(){
		log.info("enter initEdit method");
		Pagination page = new Pagination();
		String dicEntprodProfileId = request.getParameter("dicEntprodProfileId");
		try {
			if(StringUtils.isNotBlank(dicEntprodProfileId)){
				DicEntprodProfile dicEntprodProfile = dicEntprodProfileManager.getDicEntprodProfileById(dicEntprodProfileId);
				page.setRows(dicEntprodProfile);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author 徐国鹏
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
	
	
	/**
	 * 
	 *@category 新增或者编辑
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@RequestMapping(value={"/add","/edit"})
	@ResponseBody
	public Pagination dicEntprodProfileUpdate(DicEntprodProfile dicEntprodProfile){
		log.info("enter dicEntprodProfileUpdate method");
		Pagination page = new Pagination();
		try {
			SysUser user= (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			dicEntprodProfile.setUpdUser(user.getId());
			dicEntprodProfile.setUpdDatetime(ParseDate.parseFullFormat(new Date()));
			page.setForm(dicEntprodProfile);
			int count = dicEntprodProfileManager.checkRepeat(page);
			if(count>0){
				page.setMsg("名称重复！");
				page.setSuccess(false);
				return page;
			}
			dicEntprodProfileManager.dicEntprodProfileUpdate(page);
			page.setMsg("操作成功！");
			page.setSuccess(true);
		} catch (Exception e) {
			page.setMsg("操作失败！");
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category 导出excel
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@RequestMapping("exportExcel")
	@ResponseBody
	public Pagination exportExcel(HttpServletResponse response,String fileName){
		printParams();
		Pagination page = new Pagination();
		String indiName = request.getParameter("indiName");
		String status = request.getParameter("status");
		if(StringUtils.isNotBlank(indiName)){
			page.getConditions().put("indiName", indiName.trim());
		}
		if(StringUtils.isNotBlank(status)){
			page.getConditions().put("status", status.trim());
		}
		try {
			response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("GB2312"),"iso8859-1")); 
			OutputStream os= new BufferedOutputStream(response.getOutputStream());  
			 response.setContentType("application/vnd.ms-excel;charset=utf-8");  
			 dicEntprodProfileManager.getDicEntprodProfileNoPage(page);
			 String title = "药品资质类型";
			 String[] headers = {"药品资质类型编码","药品资质类型名称","状态"};
			 String[] alias = {"firstLevelIndiCode","firstLevelIndiName","status"};
			 ExcelUtil.exportExcel(page, os, title, headers, alias);
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
}
